package ray.mgocc.net;

import java.util.Map;
import java.util.Properties;
public class Request{
    private String name = null;
    private String uri;
    private Properties param = null;
    private Method method = Method.GET;
    public Request(String uri){
        setUri(uri);
    }
    public Request(String uri,String name){
        this(uri);
        setName(name);
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public void setUri(String uri){
        this.uri = uri;
    }
    public String getUri(){
        String uri = this.uri;
        if(Method.GET == this.method){
            uri = uri + "?" + getQuery();
        }
        return uri;
    }
    public String getQuery(){
        StringBuffer query = new StringBuffer();
        for(Map.Entry entry:getParam().entrySet()){
            if(0 != query.length()){
                query.append("&");
            }
            query.append(entry.getKey()).append("=").append(entry.getValue());
        }
        return query.toString();
    }
    public void addParam(String key,String value){
        if(null==param){
            param = new Properties();
        }
        param.setProperty(key,value);
    }
    public void addParam(Properties param){
        for(Map.Entry entry:param.entrySet()){
            addParam((String)entry.getKey(),(String)entry.getValue());
        }
    }
    public void setParam(Properties param){
        this.param = null;
        addParam(param);
    }
    public Properties getParam(){
        return this.param;
    }
    protected void setMethod(Method method){
        this.method = method;
    }
    protected Method getMethod(){
        return this.method;
    }
}
enum Method { GET,POST}
